import { Inject, Post, Body } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { ClassClassAppointEntity } from '../../entity/classAppoint';
import { ClassClassAppointService } from '../../service/classAppoint';
import { ClassInfoEntity } from '../../entity/info';
import { StudentStudentEntity } from '../../../student/entity/student';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';

/**
 * 班级预约
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: ClassClassAppointEntity,
  service: ClassClassAppointService,
  pageQueryOp: {
    select: ['a.*', 'b.className', 'c.name', 'd.name as operatorName'],
    join: [
      {
        entity: ClassInfoEntity,
        alias: 'b',
        condition: 'a.classId = b.id',
        type: 'leftJoin',
      },
      {
        entity: StudentStudentEntity,
        alias: 'c',
        condition: 'a.studentId = c.id',
        type: 'leftJoin',
      },
      {
        entity: BaseSysUserEntity,
        alias: 'd',
        condition: 'a.lastOperator = d.id',
        type: 'leftJoin',
      },
    ],
    fieldEq: ['a.classId', 'a.studentId'],
    where: ctx => {
      const { appointTime } = ctx.request.body;
      if (appointTime && appointTime.length > 0) {
        return [
          [
            'a.appointTime BETWEEN :start AND :end',
            { start: appointTime[0], end: appointTime[1] },
          ],
        ];
      }
      return [];
    },
  },
})
export class AdminClassClassAppointController extends BaseController {
  @Inject()
  classClassAppointService: ClassClassAppointService;

  /**
   * 预约班级
   */
  @Post('/appointClass')
  async appointClass(@Body() param: {
    studentId: number;  // 学生ID
    classId: number;    // 班级ID
  }) {
    return await this.classClassAppointService.appointClass(param);
  }

  /**
   * 取消预约
   */
  @Post('/cancelAppoint')
  async cancelAppoint(@Body() param: {
    id: number;  // 预约记录ID
  }) {
    return await this.classClassAppointService.cancelAppoint(param.id);
  }

  /**
   * 获取班级预约学员列表
   */
  @Post('/getClassStudents')
  async getClassStudents(@Body() param: {
    classId: number;  // 班级ID
  }) {
    return await this.classClassAppointService.getClassStudents(param.classId);
  }

  /**
   * 班级学员签到
   */
  @Post('/signIn')
  async signIn(@Body() param: {
    appointId: number;  // 预约记录ID
  }) {
    return await this.classClassAppointService.signIn(param.appointId);
  }
}
